import { Directive } from 'vue'
import { cacheUtils, LocalCacheKeys } from '@gyk/gyk-utils';

const hasRoles: Directive<HTMLElement, string | string[]> = (el, binding) => {
  const { value } = binding
    const super_admin = "admin";
    const roles = cacheUtils.localCache.getJSON(LocalCacheKeys.LOCAL_ROLES_KEY)

    if (value && value instanceof Array && value.length > 0) {
      const roleFlag = value

      const hasRole = roles.some(role => {
        return super_admin === role || roleFlag.includes(role)
      })

      if (!hasRole) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error(`请设置角色权限标签值"`)
    }
}

export default hasRoles